// pages/login/index.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    username: '',
    password: ''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    wx.request({
      url: 'https://api.weixin.qq.com/wxa/generate_urllink?access_token=ACCESS_TOKEN', //仅为示例，并非真实的接口地址
      data: {
          "path": "/pages/login/index",
          "query": "",
          "is_expire":true,
          "expire_type":1,
          "expire_interval":1,
          "cloud_base":
          {
              "env": "cloud1-9g5lqt9ibc64c0d5",
          }
      },
      header: {
        'content-type': 'application/json' // 默认值
      },
      success (res) {
        console.log(res.data)
      }
    })
  },

 // 登录页事件
//事件处理函数


// 获取输入账号 
usernameInput: function (e) {
  this.setData({
    username: e.detail.value
  })
},

// 获取输入密码 
passwordInput: function (e) {
  this.setData({
    password: e.detail.value
  })
},

// 登录处理
login: function () {
  var that = this;
  if (this.data.username.length == 0 || this.data.password.length == 0) {
    wx.showToast({
      title: '账号或密码不能为空',
      icon: 'none',
      duration: 2000
    })
  } else {
    const db = wx.cloud.database()
    db.collection('login').where({
      name: this.data.username,
      key: this.data.password
    })
    .get({
      success: function(res) {
        // res.data 是包含以上定义的两条记录的数组
        if(res.data.length==0) 
        return  wx.showToast({
          title: '账号或密码错误',
          icon: 'none',
          duration: 2000
        })
        else {
          //console.log()
          if(res.data[0].role=="administrators"){
            wx.showToast({
              title: '登陆成功',
              icon: 'none',
              duration: 2000
            })
            wx.setStorage({
              key:"key",
              data: {
                school: "",
              login_code: "",
              flag:false,
              },
              success: (result)=>{
                wx.switchTab({
                  url: '/pages/index/index',
                });
              }
            });
          }
          else if(res.data[0].role=="user"){
            wx.showToast({
              title: '登陆成功',
              icon: 'none',
              duration: 2000
            })
            console.log(res.data[0].school)
            let school = res.data[0].school
            let login_code = res.data[0].login_code
            wx.setStorage({
              key:"key",
              data: {
                school: school,
              login_code: login_code,
              flag:true,
              },
              success: (result)=>{
                wx.switchTab({
                  url: '/pages/index/index',
                });
              }
            });
          //   wx.switchTab({
          //     url: '/pages/index/index',
          //     success: function(res) {
          //       // 通过eventChannel向被打开页面传送数据
          //       res.eventChannel.emit('acceptDataFromOpenerPage', { role: 'user',
          //     school: school,
          //   flag: true,
          // login_code: login_code })
          //     }
          //   })
          }
          
        }
      }
    })
  }
}
})